class Solution {
public:
    int numSquares(int n) {
        if (n==0) return 0;
        vector<int> f(n+1);

        for (int i=1;i<=n;i++)
        {
            int minN = 1e9;
            for (int j=1;j*j<=i;j++)
            {
                minN = min(minN,f[i-j*j]);
            }
            f[i]=minN+1;
        }

        return f[n];
    }
};